package xhwl.insurance.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.util.List;

import xhwl.insurance.R;
import xhwl.insurance.http.bean.CommentBean;

public class CommentAdapter extends BaseAdapter {

	private int resourceId;			
	private Context context;
	private Handler handler;
	private List<CommentBean> list;
	private LayoutInflater inflater;
	public CommentAdapter(Context context, List<CommentBean> list
			, int resourceId, Handler handler){
		this.list = list;
		this.context = context;
		this.handler = handler;
		this.resourceId = resourceId;
		inflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CommentBean bean = list.get(position);
		ViewHolder holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(resourceId, null);
			holder.commentItemImg = (ImageView) convertView.findViewById(R.id.commentItemImg);
			holder.commentNickname = (TextView) convertView.findViewById(R.id.commentNickname);
			holder.commentItemTime = (TextView) convertView.findViewById(R.id.commentItemTime);
			holder.commentItemContent = (TextView) convertView.findViewById(R.id.commentItemContent);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		InputStream is = context.getResources().openRawResource(bean.getCommentImgId());
		Bitmap bitmap = BitmapFactory.decodeStream(is);
		holder.commentItemImg.setImageBitmap(bitmap);
		holder.commentNickname.setText(bean.getCommentNickname());
		holder.commentItemTime.setText(bean.getCommentTime());
		holder.commentItemContent.setText(bean.getCommentContent());
		

		
		return convertView;
	}
	
	private final class ViewHolder{
		public ImageView commentItemImg;			//评论人图片
		public TextView commentNickname;			//评论人昵称
		public TextView commentItemTime;			//评论时间
		public TextView commentItemContent;			//评论内容
	}

	


}
